#include "stream.h"

#include <stdio.h>
#include <malloc.h>

static void tl_bs_free_ud(tl_byte_source_pullable* self)
{
	free(self->ud);
}

int tl_bs_file_source(tl_byte_source_pullable* src, char const* path)
{
	FILE* f = fopen(path, "rb");
	size_t len;
	uint8* buf;
	if(!f) return -1;

	tl_bs_init_source(src);
	
	fseek(f, 0, SEEK_END);
	len = ftell(f);
	fseek(f, 0, SEEK_SET);

	buf = malloc(len);
	fread(buf, 1, len, f);

	src->ud = buf;
	src->buf = buf;
	src->buf_end = buf + len;
	src->free = tl_bs_free_ud;
	return 0;
}


int dummy_pull(tl_byte_source_pullable* self)
{
	(void)self;
	return 1;
}

void dummy_free(tl_byte_source_pullable* self)
{
	(void)self;
	// Do nothing
}

void tl_bs_init_source(tl_byte_source_pullable* self)
{
	self->pull = dummy_pull;
	self->free = dummy_free;
}


void tl_bs_free(tl_byte_source_pullable* src)
{
	src->free(src);
}